var imgFile = []; //文件流
var imgSrc = []; //图片路径
var imgName = []; //图片名字

function imageUploaderSetup() {

    imgFile = []; //文件流
    imgSrc = []; //图片路径
    imgName = []; //图片名字

    $('#imageUploaderContainer').show();
    $('#videoUploaderContainer').hide();

    $('#imageUploaderContainer .content-img-list li').each(function (index, value) {
        imgFile.push(new File(['text1', 'text2'], $(value).find('img').attr('src'), {type: 'text/plain'}));
        console.log($(value).find('img').attr('src'));
        imgSrc.push($(value).find('img').attr('src'));
        imgName.push('test.txt');
    });

    // 鼠标经过显示删除按钮
    $('#imageUploaderContainer .content-img-list').on('mouseover', '.content-img-list-item', function () {
        $(this).children('a').removeClass('layui-hide');
    });
    // 鼠标离开隐藏删除按钮
    $('#imageUploaderContainer .content-img-list').on('mouseleave', '.content-img-list-item', function () {
        $(this).children('a').addClass('layui-hide');
    });
    // 单个图片删除
    $("#imageUploaderContainer .content-img-list").on("click", '.content-img-list-item a', function () {
        var index = $(this).attr("index");
        imgSrc.splice(index, 1);
        imgFile.splice(index, 1);
        imgName.splice(index, 1);
        var boxId = ".content-img-list";
        addImageNewContent(boxId);
        $('.content-img .file').show();
        // if(imgSrc.length<10){//显示上传按钮
        // 	$('.content-img .file').show();
        // }
    });
    //图片上传
    $('#imageUploader').on('change', function () {

        // if(imgSrc.length>=10){
        // 	return alert("最多只能上传4张图片");
        // }
        var imgSize = this.files[0].size;  //b
        if (imgSize > 1024 * 1024 * 5) {//1M
            return alert("上传图片不能超过5M");
        }

        if (this.files[0].type != 'image/png' && this.files[0].type != 'image/jpeg' && this.files[0].type != 'image/gif') {
            return alert("图片上传格式不正确");
        }

        var imgBox = '.content-img-list';
        var fileList = this.files;
        for (var i = 0; i < fileList.length; i++) {
            var imgSrcI = getObjectURL(fileList[i]);
            imgName.push(fileList[i].name);
            imgSrc.push(imgSrcI);
            imgFile.push(fileList[i]);
        }
        addImageNewContent(imgBox);
        this.value = null;//解决无法上传相同图片的问题
    });
}

//图片展示
function addImageNewContent(obj) {
    obj = '#imageUploaderContainer ' + obj;
    $(obj).html("");
    for (var a = 0; a < imgSrc.length; a++) {
        var oldBox = $(obj).html();
        $(obj).html(oldBox + '<li class="content-img-list-item"><img src="' + imgSrc[a] + '" alt=""><a index="' + a + '" class="layui-hide delete-btn"><i class="layui-icon layui-icon-delete"></i></a></li>');
    }
}

//建立一個可存取到該file的url
function getObjectURL(file) {
    var url = null;
    if (window.createObjectURL != undefined) { // basic
        url = window.createObjectURL(file);
    } else if (window.URL != undefined) { // mozilla(firefox)
        url = window.URL.createObjectURL(file);
    } else if (window.webkitURL != undefined) { // webkit or chrome
        url = window.webkitURL.createObjectURL(file);
    }
    return url;
}